{% extends "base_generic.html" %}
{% load static %}
{% block extra_css %}
<link rel="stylesheet" href="{% static 'css/user_management.css' %}">
{% endblock %}

{% block content %}
<div class="container mt-4">
    <h1 class="text-center mb-4">用户管理</h1>

    <!-- 用户列表卡片 -->
    <div class="card mb-4">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h2 class="mb-0">用户列表</h2>
            <button id="addUserBtn" class="btn btn-primary btn-sm">添加用户</button>
        </div>
        
        <div class="card-body">
            <table class="table table-striped">
                <thead>
                    <tr>
                        <th scope="col">用户名</th>
                        <th scope="col">邮箱</th>
                        <th scope="col">创建时间</th>
                        <th scope="col">用户类型</th>
                        <th scope="col">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for user in users %}
                    <tr>
                        <td>{{ user.username }}</td>
                        <td>{{ user.email }}</td>
                        <td>{{ user.date_joined|date:"Y-m-d H:i" }}</td>
                        <td>{% if user.is_staff %}管理员{% else %}普通用户{% endif %}</td>
                        <td>
                            <a href="{% url 'delete-user' user.id %}" class="btn btn-danger btn-sm">删除</a>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    <!-- 添加用户卡片 (默认隐藏) -->
    <div id="addUserForm" class="card mb-4" style="display:none;">
        <div class="card-header">
            <h4>添加用户</h4>
        </div>
        <div class="card-body">
            <form method="post" action="{% url 'add-user' %}">
                {% csrf_token %}
                <div class="row">
                    <div class="col-md-4 mb-3">
                        <label for="username" class="form-label">用户名:</label>
                        <input type="text" id="username" name="username" class="form-control" required>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label for="email" class="form-label">邮箱:</label>
                        <input type="email" id="email" name="email" class="form-control" required>
                    </div>
                    <div class="col-md-4 mb-3">
                        <label for="password" class="form-label">密码:</label>
                        <input type="password" id="password" name="password" class="form-control" required>
                    </div>
                </div>
                <div class="text-end">
                    <button type="submit" class="btn btn-primary">添加</button>
                    <button type="button" class="btn btn-secondary" onclick="hideForm('addUserForm')">取消</button>
                </div>
            </form>
        </div>
    </div>

    {% if messages %}
    <div class="mt-4">
        {% for message in messages %}
        <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
            {{ message }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        {% endfor %}
    </div>
    {% endif %}
</div>

<script>
    // 显示添加用户表单
    document.getElementById('addUserBtn').addEventListener('click', function() {
        document.getElementById('addUserForm').style.display = 'block';
    });

    // 隐藏表单
    function hideForm(formId) {
        document.getElementById(formId).style.display = 'none';
    }
</script>
{% endblock %}